package com.tipfy.tmx;


class TiledAttributes
{
    /**
     *
     */
    public static function get(xml:Xml, name:String,
                               ?defaultValue:String=null,
                               ?required:Bool=true):String
    {
        var value:String = xml.get(name);

        if (value == null)
        {
            if (defaultValue != null)
            {
                return defaultValue;
            }
            else if(required == true)
            {
                throw "Missing attribute '" + name + "' in <" +
                    xml.nodeName + ">.";
            }
        }

        return value;
    }

    /**
     *
     */
    public static function getNonEmpty(xml:Xml, name:String,
                                       ?defaultValue:String=null,
                                       ?required:Bool=true):String
    {
        var value:String = TiledAttributes.get(xml, name, defaultValue,
            required);

        if (value != null && value.length < 1)
        {
            throw "Attribute '" + name + "' in <" + xml.nodeName +
                "> can't be empty.";
        }

        return value;
    }

    /**
     *
     */
    public static function getFloat(xml:Xml, name:String,
                                    ?defaultValue:String=null,
                                    ?required:Bool=true):Float
    {
        return Std.parseFloat(TiledAttributes.get(xml, name, defaultValue,
            required));
    }

    /**
     *
     */
    public static function getInt(xml:Xml, name:String,
                                  ?defaultValue:String=null,
                                  ?required:Bool=true):Int
    {
        return Std.parseInt(TiledAttributes.get(xml, name, defaultValue,
            required));
    }

    /**
     *
     */
    public static function getNonNegativeInt(xml:Xml, name:String,
                                             ?defaultValue:String=null,
                                             ?required:Bool=true):Int
    {
        var value:Int = TiledAttributes.getInt(xml, name, defaultValue,
            required);

        if (value < 0)
        {
            throw "Attribute '" + name + "' in <" + xml.nodeName +
                "> must be a non-negative Int.";
        }

        return value;
    }

    /**
     *
     */
    public static function getPositiveInt(xml:Xml, name:String,
                                          ?defaultValue:String=null,
                                          ?required:Bool=true):Int
    {
        var value:Int = TiledAttributes.getInt(xml, name, defaultValue,
            required);

        if (value < 1)
        {
            throw "Attribute '" + name + "' in <" + xml.nodeName +
                "> must be a positive Int.";
        }

        return value;
    }

    /**
     *
     */
    public static function getEnum(xml:Xml, name:String,
                                   validValues:Array<String>,
                                   ?defaultValue:String=null,
                                   ?required:Bool=true):String
    {
        var value:String = TiledAttributes.get(xml, name, defaultValue,
            required);

        if (value != null)
        {
            for (valid in validValues.iterator())
            {
                if (value == valid)
                {
                    return value;
                }
            }

            throw "Attribute '" + name + "' in <" + xml.nodeName +
                    "> has not a valid value.";
        }

        return value;
    }
}
